package com.example.firstdemo.ui.screens.pages.login

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SecureTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.firstdemo.R
import com.example.firstdemo.ui.screens.components.MessageBox
import com.example.firstdemo.ui.theme.AppTheme
import com.example.firstdemo.ui.theme.FontSize
import com.example.firstdemo.ui.theme.Space
import com.example.firstdemo.ui.theme.eColors


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LoginScreen(
    uiState: LoginUiState,
    onLogin: (userName: String, password: String) -> Unit,
    onMessageConfirm: () -> Unit = {},
    padding: PaddingValues = PaddingValues(0.dp)
) {
    val userName = rememberTextFieldState()
    val password = rememberTextFieldState()

    Surface(
        modifier = Modifier
            .padding(padding)
            .fillMaxSize()
    ) {
        Column(Modifier.fillMaxHeight()) {
            Spacer(Modifier.weight(1f))
            Image(
                painter = painterResource(R.drawable.login_bg),
                contentDescription = "",
            )
        }
        Column(
            Modifier
                .fillMaxSize()
                .padding(start = Space.xxl, end = Space.xxl)
        ) {
            Spacer(Modifier.height(50.dp))
            Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
                Image(
                    painterResource(R.drawable.logo),
                    "",
                    modifier = Modifier.size(150.dp)
                )
            }
            Surface(
                shape = MaterialTheme.shapes.medium,
                border = BorderStroke(1.dp, Color(0xfff2f2f2)),
                modifier = Modifier.fillMaxWidth()
            ) {
                Column {
                    TextField(
                        state = userName,
                        placeholder = {
                            Text("用户名", color = Color(0xff808080))
                        },
                        colors = TextFieldDefaults.colors(
                            unfocusedContainerColor = Color.White,
                            focusedContainerColor = Color.White,
                            unfocusedIndicatorColor = Color.Transparent,
                            focusedIndicatorColor = Color.Transparent
                        ),
                        modifier = Modifier.fillMaxWidth()
                    )
                    HorizontalDivider(
                        Modifier
                            .height(1.dp)
                            .border(1.dp, Color(0xfff2f2f2))
                    )
                    SecureTextField(
                        state = password,
                        placeholder = {
                            Text("密码", color = Color(0xff808080))
                        },
                        colors = TextFieldDefaults.colors(
                            unfocusedContainerColor = Color.White,
                            focusedContainerColor = Color.White,
                            unfocusedIndicatorColor = Color.Transparent,
                            focusedIndicatorColor = Color.Transparent
                        ),
                        modifier = Modifier.fillMaxWidth()
                    )
                }
            }
            Spacer(Modifier.height(Space.medium))
            Button(
                shape = MaterialTheme.shapes.medium,
                onClick = {
                    onLogin(userName.text.toString(), password.text.toString())
                },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(40.dp)
            ) {
                Text(
                    text = "登录",
                    fontSize = FontSize.large,
                    color = MaterialTheme.eColors.basic.white
                )
            }
        }
        Column {
            if (uiState.errorMessage !== null) {
                MessageBox(
                    title = "错误",
                    info = uiState.errorMessage,
                    onConfirm = {
                        onMessageConfirm()
                    }
                )
            }
        }
    }
}

@Preview(
    showBackground = true,
    showSystemUi = true
)
@Composable
fun LoginScreenPreview() {
    AppTheme() {
        LoginScreen(
            LocalLoginDataProvider.uiState,
            { a, b -> Unit }
        )
    }
}